{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "    此代码用python完成快速排序算法\n",
    "    author: 算法导论\n",
    "    time: 2018-07-27\n",
    "    时间复杂度： O(nlog(n))\n",
    "    目标：\n",
    "'''\n",
    "def quick_sort(array, l, r):\n",
    "    if l < r:\n",
    "        q = partition(array, l, r)\n",
    "        quick_sort(array, l, q - 1)\n",
    "        quick_sort(array, q + 1, r)\n",
    "    return array\n",
    " \n",
    "def partition(array, l, r):\n",
    "    x = array[r]\n",
    "    i = l - 1\n",
    "    for j in range(l, r):\n",
    "        if array[j] <= x:\n",
    "            i += 1\n",
    "            array[i], array[j] = array[j], array[i]\n",
    "    array[i + 1], array[r] = array[r], array[i+1]\n",
    "    return i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 5, 7]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lists = [3,5,1,2,7]\n",
    "quick_sort(lists, 0, len(lists)-1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
